import express from 'express';
import { StudentController } from '../controllers/studentController.js';
import { authenticateToken } from '../middleware/auth.js';
import { upload } from '../middleware/upload.js';
import { requirePermission, attachUserInfo } from '../middleware/rbac.js';

const router = express.Router();
const studentController = new StudentController();

// 所有路由都需要认证
router.use(authenticateToken);
router.use(attachUserInfo);

// 获取学生表单选项数据
router.get('/form-options', studentController.getFormOptions.bind(studentController));

// 获取用于成绩管理的学生列表
router.get('/for-grades', studentController.getStudentsForGrades.bind(studentController));

// 获取学生列表 - 教师和管理员都可以查看
router.get('/', requirePermission('student:read'), studentController.getStudents.bind(studentController));

// 创建学生 - 仅管理员
router.post('/', requirePermission('student:create'), studentController.createStudent.bind(studentController));

// 更新学生 - 仅管理员
router.put('/:id', requirePermission('student:update'), studentController.updateStudent.bind(studentController));

// 删除学生 - 仅管理员
router.delete('/:id', requirePermission('student:delete'), studentController.deleteStudent.bind(studentController));

// 添加缺失的路由
// 获取学生评语
router.get('/:id/comments', studentController.getStudentComments.bind(studentController));

// 添加学生评语
router.post('/:id/comments', studentController.addStudentComment.bind(studentController));

// 删除学生评语
router.delete('/:id/comments/:stage', studentController.deleteStudentComment.bind(studentController));

// 获取评语阶段选项
router.get('/comments/stages', studentController.getCommentStages.bind(studentController));

// 导出学生数据 - 仅管理员
router.get('/export', requirePermission('student:export'), studentController.exportStudents.bind(studentController));

// 导入学生数据（使用multer处理文件上传） - 仅管理员
router.post('/import', requirePermission('student:import'), upload.single('file'), studentController.importStudents.bind(studentController));

// 下载导入模板 - 仅管理员
router.get('/import/template', requirePermission('student:import'), studentController.downloadImportTemplate.bind(studentController));

export default router;